﻿using DKT.WeiXinAPI.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace DKT.WeiXinAPI
{
    public class Handler
    {
        public static string GetMessage()
        {
            try
            {
                var message = Factory.ApiFactory.CreareMessage(); //.Reply();
                foreach(var m in message)
                {
                    var r = m.Reply();
                    if(r!=null)
                    {
                        return SerializeToWeiXinXml(r);
                    }
                }
            }
            catch (Exception e)
            {
                //log
            }

            return "";
        }





        /// <summary>
        /// 注册消息
        /// </summary>
        /// <param name="message"></param>
        public static void Register(Type message)
        {
            object[] attribs = message.GetCustomAttributes(true);
            if(attribs.Length!=0)
            {
                foreach(object attrib in attribs)
                {
                    if(attrib is EventAttribute)
                    {
                        EventAttribute ea = attrib as EventAttribute;

                        if (ea.MsgType != MsgType.Event)
                        {
                            if (!ApiFactory.MessageRegister.ContainsKey(ea.MsgType))
                            {
                                IList<Type> am = new List<Type>();
                                am.Add(message);
                                ApiFactory.MessageRegister.Add(ea.MsgType, am);
                            }
                            else
                            {
                                ApiFactory.MessageRegister[ea.MsgType].Add(message);
                            }
                        }
                        else
                        {

                            if (!ApiFactory.EventRegister.ContainsKey(ea.EventType))
                            {
                                IList<Type> am = new List<Type>();
                                am.Add(message);
                                ApiFactory.EventRegister.Add(ea.EventType, am);
                            }
                            else
                            {
                                ApiFactory.EventRegister[ea.EventType].Add(message);
                            }

                        }
                    }
                }
            }
        }



        /// <summary>
        /// 卸载消息处理器
        /// </summary>
        /// <param name="message"></param>
        public static void UnRegister(Type type)
        {
            object[] attribs = type.GetCustomAttributes(true);
            if (attribs.Length != 0)
            {
                foreach (object attrib in attribs)
                {
                    if (attrib is EventAttribute)
                    {
                        EventAttribute ea = attrib as EventAttribute;

                        if (ea.MsgType != MsgType.Event)
                        {
                            if (ApiFactory.MessageRegister.ContainsKey(ea.MsgType))
                            {
                                IList<Type> am = ApiFactory.MessageRegister[ea.MsgType];
                                am.Remove(type);
                            }
                        }else
                        {
                            if (ApiFactory.EventRegister.ContainsKey(ea.EventType))
                            {
                                IList<Type> am = ApiFactory.EventRegister[ea.EventType];
                                am.Remove(type);
                            }
                        }
                    }
                }
            }
        }







        /// <summary>
        /// 序列化对象为微信需要的XML
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToWeiXinXml(object obj)
        {
            if (obj == null) { return ""; }
            Type type = obj.GetType();
            if (type.IsSerializable)
            {
                try
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);


                    XmlWriterSettings xset = new XmlWriterSettings();

                    xset.OmitXmlDeclaration = true;
                    xset.CloseOutput = true;
                    xset.Encoding = Encoding.UTF8;
                    xset.Indent = true;
                    xset.CheckCharacters = false;
                    System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, xset);


                    xs.Serialize(xw, obj, ns);
                    xw.Flush();
                    xw.Close();
                    return sb.ToString();
                }
                catch { return ""; }
            }
            else
            {
                return "";
            }
        }
    }
}
